package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.PageList;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.TProdInv;
import com.ruoyi.system.domain.TProdInvRecord;
import com.ruoyi.system.domain.dto.ITProdInvDTO;
import com.ruoyi.system.domain.vo.TProdInvRecordVO;
import com.ruoyi.system.mapper.TProdInvRecordMapper;
import com.ruoyi.system.service.ISysAdminUserService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.ITProdInvRecordService;
import com.ruoyi.system.service.ITProdInvService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wux
 * @date 2023/5/31 16:07
 */
@Service
public class TProdInvRecordServiceImpl extends ServiceImpl<TProdInvRecordMapper, TProdInvRecord> implements ITProdInvRecordService {
    @Autowired
    private ISysAdminUserService adminUserService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ITProdInvService prodInvService;
    @Autowired
    private ITProdInvRecordService prodInvRecordService;

    @Override
    public PageList<TProdInvRecordVO> listByPage(ITProdInvDTO.ITProdInvRecordDTO params) {
        Page<Object> objects = PageHelper.startPage(params.getPageNo(), params.getPageSize());

        List<TProdInvRecord> list = this.list(new LambdaQueryWrapper<TProdInvRecord>()
                .eq(params.getId() != null, TProdInvRecord::getId, params.getId())
                .eq(params.getInvId() != null, TProdInvRecord::getInvId, params.getInvId())
                .eq(params.getType() != null, TProdInvRecord::getType, params.getType()));
        List<TProdInvRecordVO> listVO = new ArrayList<>();
        list.forEach(item -> {
            TProdInvRecordVO vo = new TProdInvRecordVO();
            BeanUtils.copyProperties(item, vo);
            if (vo.getCreateBy() > 100000L) {
                vo.setCreateByName(sysUserService.getById(vo.getCreateBy()).getName());
            } else {
                vo.setCreateByName(adminUserService.getById(vo.getCreateBy()).getName());
            }
            listVO.add(vo);
        });
        PageList<TProdInvRecordVO> pageList = PageUtil.getPageList(listVO);
        pageList.setTotal(objects.getTotal());
        return pageList;
    }

    @Override
    public void outProd(ITProdInvDTO.ITProdInvOutDTO params) {
        // 更新库存表
        TProdInv prodInv = prodInvService.getById(params.getInvId());
        prodInv.setOutNum(prodInv.getOutNum() + params.getOutNum());
        prodInv.setInventory(prodInv.getInventory() - params.getOutNum());
        prodInvService.updateById(prodInv);
        // 进行出库操作
        TProdInvRecord invRecord = new TProdInvRecord();
        invRecord.setInvId(prodInv.getId());
        invRecord.setType(2);
        invRecord.setOperateNum(params.getOutNum());
        invRecord.setConsignee(params.getConsignee());
        invRecord.setSignImage(params.getSignImage());
        invRecord.setRemark(params.getRemark());
        invRecord.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        invRecord.setUpdateBy(SecurityUtils.getLoginUser().getUser().getId());
        invRecord.setUsedPart(params.getUsedPart());
        prodInvRecordService.save(invRecord);
    }
}
